package it.osg.pipe.sourcegenerators;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import it.osg.pipe.core.SourceGenerator;
import it.osg.utils.JSONObjectUtil;

public class FacebookSourceGenerator extends SourceGenerator {

	public FacebookSourceGenerator(String modName, String conFilePath) {
		super(modName, conFilePath);
		System.setProperty("java.net.useSystemProxies", "true");
	}

	@Override
	public ArrayList<String> add() {
		ArrayList<String> result = new ArrayList<String>();

		InputStream is = null;
		try {
			is = new URL(config.get("url")).openStream();
			try {
				BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
				String jsonString = readAll(rd);
				System.out.println(jsonString);
				Object objJson = JSONValue.parse(jsonString);
				JSONObject json =(JSONObject) objJson;
				System.out.println(JSONObjectUtil.retrieveJsonPath(json, "key"));
			} finally {
				is.close();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}



		return result;
	}



	private static String readAll(Reader rd) throws IOException {
		StringBuilder sb = new StringBuilder();
		int cp;
		while ((cp = rd.read()) != -1) {
			sb.append((char) cp);
		}
		return sb.toString();
	}



	public static void main(String[] args) {
		System.out.println("=======Esempio Json Parser=======");

		String s="{\"glossary\": {" +
				"\"title\": \"example glossary\"," +
				"\"GlossDiv\": {" +
				"\"title\": \"S\"," +
				"\"GlossList\": {" +
				"\"GlossEntry\": {" +
				"\"ID\": \"SGML\"," +
				"\"SortAs\": \"SGML\"," +
				"\"GlossTerm\": \"Standard Generalized Markup Language\"," +
				"\"Acronym\": \"SGML\"," +
				"\"Abbrev\": \"ISO 8879:1986\"," +
				"\"GlossDef\": {" +
				"\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\"," +
				"\"GlossSeeAlso\": [\"GML\", \"XML\"]" +
				"}," +
				"\"GlossSee\": \"markup\"" +
				"}" +
				"}" +
				"}" +
				"}" +
				"}";


		Object obj = JSONValue.parse(s);
		JSONObject json =(JSONObject) obj;
		System.out.println(JSONObjectUtil.retrieveJsonPath(json, "glossary/GlossDiv/GlossList/GlossEntry/SortAs"));

//		System.setProperty("proxySet", "true");
//		System.setProperty("http.proxyHost", "proxy host addr");
//		System.setProperty("http.proxyPort", "808");
//		Authenticator.setDefault(new Authenticator() {
//		    protected PasswordAuthentication getPasswordAuthentication() {
//
//		        return new PasswordAuthentication("domain\\user","password".toCharArray());
//		    }
//		});
//		
		System.setProperty("java.net.useSystemProxies", "true");
		
		URL oracle;
		try {
			oracle = new URL("http://www.oracle.com/");
			BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

			String inputLine;
			while ((inputLine = in.readLine()) != null)
				System.out.println(inputLine);
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}


	}

}
